package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.Product;
import com.newway.back.mapper.ProductMapper;
import com.newway.common.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/product")
public class ProductController {

    @Resource
    ProductMapper productMapper;

    @GetMapping("/list")
    public Result getList(Product product){
        Page<Product> productPage = new Page<>(product.getPageNum(),product.getPageSize());
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(product.getStatus())){
            productQueryWrapper.eq("status",product.getStatus());
        }
        if(StringUtils.isNotBlank(product.getProductName())){
            productQueryWrapper.like("product_name",product.getProductName());
        }
        if(StringUtils.isNotBlank(product.getProductType())){
            productQueryWrapper.like("product_type",product.getProductType());
        }
        Page<Product> result = productMapper.selectPage(productPage, productQueryWrapper);
        return Result.success(result);
    }

    @PostMapping("/add")
    public Result addProduct(@RequestBody Product product){
        productMapper.insert(product);
        return Result.success();
    }

    @PostMapping("/update")
    public Result updateProduct(@RequestBody Product product){
        productMapper.updateById(product);
        return Result.success();
    }

    @PostMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        productMapper.deleteById(id);
        return Result.success();
    }

    @PostMapping("/statusUpdate/{id}/{status}")
    public Result upProduct(@PathVariable("id") Integer id,@PathVariable("status") String status){
        Product product = productMapper.selectById(id);
        if(product!=null){
            product.setStatus(status);
            productMapper.updateById(product);
        }
        return Result.success();
    }

    @PostMapping("/addStock/{id}/{sum}")
    public Result addStock(@PathVariable("id") Integer id,@PathVariable("sum")Integer sum){
        Product product = productMapper.selectById(id);
        if(product!=null){
            product.setBalanceCount(product.getBalanceCount()+sum);
            productMapper.updateById(product);
        }
        return Result.success();
    }

    /**
     * 获取购物车中的数据
     * @param productIds
     * @return
     */
    @PostMapping("/shopCart")
    public Result shopCarList(@RequestBody List<Integer> productIds){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",productIds);
        queryWrapper.in("status","1");
        List<Product> products = productMapper.selectList(queryWrapper);
        return  Result.success(products);
    }
}
